﻿using System.Threading.Tasks;
using TabbedTemplate.Views;
using Xamarin.Forms;

namespace TabbedTemplate.Services
{
    /// <summary>
    /// 打卡导航服务。
    /// </summary>
    public class PunchNavigationService : IPunchNavigationService
    {
        //******** 私有变量
        public MainPage MainPage =>
            _mainPage ?? (_mainPage = Application.Current.MainPage as MainPage);

        /// <summary>
        /// MainPage。
        /// </summary>
        private MainPage _mainPage;

        /// <summary>
        /// 导航激活服务
        /// </summary>
        /// <param name="punchPageActivationService"></param>
        public PunchNavigationService(
            IPunchPageActivationService punchPageActivationService)
        {
            _punchPageActivationService = punchPageActivationService;
        }

        /// <summary>
        /// 导航激活服务
        /// </summary>
        private IPunchPageActivationService _punchPageActivationService;

        /// <summary>
        /// 导航到页面。
        /// </summary>
        /// <param name="pageKey">页面键。</param>
        /// <returns></returns>
        public async Task NavigationToAsync(string pageKey)
        {

            //await MainPage.Children[0].Navigation.PopAsync();
            var page = _punchPageActivationService.Activate(pageKey);
            await MainPage.Children[0].Navigation.PushAsync(page, false);
        }

        public async Task UserPageNavigationToAsync(string pageKey)
        {
            var page = _punchPageActivationService.Activate(pageKey);
            await MainPage.Children[1].Navigation.PushAsync(page);
        }

        /// <summary>
        /// 导航。
        /// </summary>
        /// <param name="pageKey">页面键。</param>
        /// <param name="parameter">参数。</param>
        public async Task NavigateToAsync(string pageKey, object parameter)
        {
            //await MainPage.Children[0].Navigation.PopAsync();
            var page = _punchPageActivationService.Activate(pageKey);
            NavigationContext.SetParameter(page, parameter);
            await MainPage.Children[0].Navigation.PushAsync(page, false);
        }

        /// <summary>
        /// 向后带参导航。
        /// </summary>
        /// <param name="pageKet">页面键。</param>
        /// <param name="parameter">参数。</param>
        /// <returns></returns>
        public async Task NavigateBackAsync(string pageKey, object parameter)
        {

            var page = _punchPageActivationService.Activate(pageKey);
            NavigationContext.SetParameter(page, parameter);
            await MainPage.Children[0].Navigation.PopAsync(false);
        }

        /// <summary>
        /// 导航到Syncfusion页面，不适用集中的缓存。
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public async Task NavigateToPunchViewPage(object parameter)
        {
            var page = new PunchViewPage();
            NavigationContext.SetParameter(page, parameter);
            var index = MainPage.Children.IndexOf(MainPage.CurrentPage);
            await MainPage.Children[index].Navigation.PushAsync(page);
        }

        public async Task NavigateToPunchPage(object parameter)
        {
            var page = new PunchPage();
            NavigationContext.SetParameter(page, parameter);
            var index = MainPage.Children.IndexOf(MainPage.CurrentPage);
            await MainPage.Children[index].Navigation.PushAsync(page);
        }

        public async Task NavigateBack()
        {
            var index = MainPage.Children.IndexOf(MainPage.CurrentPage);
            await MainPage.Children[index].Navigation.PopAsync();
        }


        public async Task UserPageNavigationToAsync(string pageKey, object parameter)
        {
            var page = _punchPageActivationService.Activate(pageKey);
            NavigationContext.SetParameter(page, parameter);
            //await MainPage.Navigation.PushAsync(page);
            await MainPage.Children[1].Navigation.PushAsync(page);
        }

        public async Task NavigateToStorePunchesPage(object parameter)
        {
            var page = new StorePunchesPage();
            NavigationContext.SetParameter(page, parameter);
            var index = MainPage.Children.IndexOf(MainPage.CurrentPage);
            await MainPage.Children[index].Navigation.PushAsync(page);
        }
    }

    public static class ContentNavigationConstant
    {
        /// <summary>
        /// 打卡页键。
        /// </summary>
        public const string PunchPage = nameof(Views.PunchPage);

        public const string StoreCollectionsPage = nameof(Views.StoreCollectionsPage);

        /// <summary>
        /// 店铺打卡内容页键。
        /// </summary>
        public const string StorePunchesPage = nameof(Views.StorePunchesPage);

        public const string IndexPage = nameof(Views.IndexPage);

        public const string QueryPage = nameof(Views.QueryPage);

        public const string QueryResultPage = nameof(Views.QueryResultPage);

        public const string CityLocations = nameof(Views.CityLocations);

        public const string NewIndexPage = nameof(Views.NewIndexPage);

        public const string PunchViewPage = nameof(Views.PunchViewPage);

        public const string SyncPage = nameof(Views.SyncPage);
        public const string MyPunchesPage = nameof(Views.MyPunchesPage);
        public const string DrawPicPage = nameof(Views.DrawPicPage);
    }
}
